package com.ourteam.product.ui.dao.impl;

import com.ourteam.product.ui.dao.IProductUIFormFieldDAO;
import com.ourteam.product.ui.dao.ProductUIFormField;
import com.ourteam.product.ui.dao.ProductUIFormFieldDAOQueryBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;

/**
 * Template Name : DAO服务对象 Template Version : 1 Template Last Modfiy Date : Fri Nov 17 11:00:11 CST
 * 2017 Auto Generate Date : 2018-12-14 15:15:17 Warning ! This file will be overwrite when
 * regenerate !
 */
public class AbstractProductUIFormFieldDAOJdbcImpl extends AbstractSpringDAO
    implements IProductUIFormFieldDAO {

  public static final String[] ENTITY_FIELDS =
      new String[] {
        Id,
        ParentId,
        FormId,
        TypeId,
        Code,
        Title,
        TitleKey,
        TipInfo,
        ReadOnly,
        AllowEmpty,
        DataSource,
        Style,
        ParameterValue,
        SortIndex,
        Status,
        Remarks,
        ModelPropertyId,
        FieldSpan,
        FieldColumn,
        RemenberValue,
        TypeWidget,
        TypeName,
        AllowSubField,
        TypeIcon,
        DataSourceCode,
        DataSourceName
      };

  public static final String[] ENTITY_PROPERTIES =
      new String[] {
        _DataObjectProperty.Id,
        _DataObjectProperty.ParentId,
        _DataObjectProperty.FormId,
        _DataObjectProperty.TypeId,
        _DataObjectProperty.Code,
        _DataObjectProperty.Title,
        _DataObjectProperty.TitleKey,
        _DataObjectProperty.TipInfo,
        _DataObjectProperty.ReadOnly,
        _DataObjectProperty.AllowEmpty,
        _DataObjectProperty.DataSource,
        _DataObjectProperty.Style,
        _DataObjectProperty.ParameterValue,
        _DataObjectProperty.SortIndex,
        _DataObjectProperty.Status,
        _DataObjectProperty.Remarks,
        _DataObjectProperty.ModelPropertyId,
        _DataObjectProperty.FieldSpan,
        _DataObjectProperty.FieldColumn,
        _DataObjectProperty.RemenberValue,
        _DataObjectProperty.TypeWidget,
        _DataObjectProperty.TypeName,
        _DataObjectProperty.AllowSubField,
        _DataObjectProperty.TypeIcon,
        _DataObjectProperty.DataSourceCode,
        _DataObjectProperty.DataSourceName
      };

  public static final String[] ENTITY_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {};

  private static String relatedJoinSQL = null;

  private static String basicFilterSQL = null;

  private DataAccessObjectHelper<ProductUIFormField, ProductUIFormFieldDAOQueryBean> daoHelper;

  public AbstractProductUIFormFieldDAOJdbcImpl() {
    super();
    daoHelper =
        new DataAccessObjectHelper<ProductUIFormField, ProductUIFormFieldDAOQueryBean>(this);
  }

  protected String getEntityName() {
    return EntityName;
  }

  protected String[] getEntityFields() {
    return ENTITY_FIELDS;
  }

  protected String[] getEntityProperties() {
    return ENTITY_PROPERTIES;
  }

  protected String[] getEntityPKFields() {
    return ENTITY_PK_FIELDS;
  }

  protected String[] getEntityAutoGenPKFields() {
    return ENTITY_AUTO_GEN_PK_FIELDS;
  }

  protected String[] getEntityUniqueFields() {
    return ENTITY_UNIQUE_FIELDS;
  }

  protected String getBaiscQueryFilter() {

    if (basicFilterSQL == null) {
      StringBuffer basicFilterSQLbuffer = new StringBuffer();

      basicFilterSQL = basicFilterSQLbuffer.toString();
    }

    return basicFilterSQL;
  }

  protected String getRelatedJoinSQL() {

    if (relatedJoinSQL == null) {
      StringBuffer relatedJoinSQLBuffer = new StringBuffer();

      relatedJoinSQLBuffer
          .append(" LEFT JOIN ")
          .append(com.ourteam.product.ui.dao.IProductUIFormFieldTypeDAO.EntityName)
          .append(" PRODUCT_UI_FORM_FIELD_TYPE ");

      relatedJoinSQLBuffer.append(" ON ( ");

      relatedJoinSQLBuffer.append(
          StringUtils.replace(
              com.ourteam.product.ui.dao.IProductUIFormFieldTypeDAO.Id,
              com.ourteam.product.ui.dao.IProductUIFormFieldTypeDAO.EntityName + ".",
              "PRODUCT_UI_FORM_FIELD_TYPE."));
      relatedJoinSQLBuffer.append(" = ").append(TypeId);

      relatedJoinSQLBuffer.append(" ) ");

      relatedJoinSQLBuffer
          .append(" LEFT JOIN ")
          .append(com.ourteam.product.ui.dao.IProductUIListDAO.EntityName)
          .append(" RELATED_GLHNE4_ProductUIList ");

      relatedJoinSQLBuffer.append(" ON ( ");

      relatedJoinSQLBuffer.append(
          StringUtils.replace(
              com.ourteam.product.ui.dao.IProductUIListDAO.Id,
              com.ourteam.product.ui.dao.IProductUIListDAO.EntityName + ".",
              "RELATED_GLHNE4_ProductUIList."));
      relatedJoinSQLBuffer.append(" = ").append(DataSource);

      relatedJoinSQLBuffer.append(" ) ");

      relatedJoinSQL = relatedJoinSQLBuffer.toString();
    }

    return relatedJoinSQL;
  }

  /**
   * Check Data Unique
   *
   * @param aProductUIFormField
   * @throws Exception
   */
  public boolean checkIsUnique(ProductUIFormField aProductUIFormField) throws Exception {

    if (aProductUIFormField == null) {
      return true;
    }

    List valueList = new ArrayList();

    List pkValueList = new ArrayList();

    pkValueList.add(aProductUIFormField.getId());

    return isUnique(
        valueList.toArray(new Object[valueList.size()]),
        pkValueList.toArray(new Object[pkValueList.size()]));
  }

  /**
   * Insert Data
   *
   * @param aProductUIFormField
   * @throws Exception
   */
  public void insert(ProductUIFormField aProductUIFormField) throws Exception {

    super.doInsert(aProductUIFormField);
  }

  /**
   * Batch Insert Datas
   *
   * @param aProductUIFormField
   * @throws Exception
   */
  public void batchInsert(ProductUIFormField[] aProductUIFormFields) throws Exception {

    super.doBatchInsert(aProductUIFormFields);
  }

  /**
   * Delete Data
   *
   * @param final long id *
   * @throws Exception
   */
  public void delete(final long id) throws Exception {

    super.doDeleteById(new Object[] {new Long(id)});
  }

  /**
   * Delete Datas
   *
   * @param queryBean
   * @throws Exception
   */
  public void delete(ProductUIFormFieldDAOQueryBean queryBean) throws Exception {

    super.doDeleteByQuery(queryBean);
  }

  public void delete(ProductUIFormField aProductUIFormField) throws Exception {

    delete(aProductUIFormField.getId());
  }

  /**
   * Update Data Selective
   *
   * @param aProductUIFormField
   * @throws Exception
   */
  public void update(ProductUIFormField aProductUIFormField) throws Exception {

    if (aProductUIFormField.getId() == 0) {
      throw new Exception("Can not update object data whith out primary key value");
    }

    super.doUpdate(aProductUIFormField);

    ProductUIFormField tempObj = findById(aProductUIFormField.getId());

    if (tempObj != null) {
      PropertyUtils.copyProperties(aProductUIFormField, tempObj);
    }
  }

  /**
   * Batch Update Datas
   *
   * @param aProductUIFormFields[]
   * @throws Exception
   */
  public void batchUpdate(ProductUIFormField[] aProductUIFormFields) throws Exception {

    super.doBatchUpdate(aProductUIFormFields);
  }

  /**
   * Update Data By Condictions
   *
   * @param aProductUIFormField
   * @param aValueCriteria
   * @param aTransaction
   * @throws Exception
   */
  public void update(
      ProductUIFormField aProductUIFormField, ProductUIFormFieldDAOQueryBean aQueryBean)
      throws Exception {

    super.doUpdateByQuery(aProductUIFormField, aQueryBean);
  }

  /**
   * Update Data By Condictions
   *
   * @param aQueryBean
   * @throws Exception
   */
  public void update(ProductUIFormFieldDAOQueryBean aQueryBean) throws Exception {
    super.doUpdateByQuery(aQueryBean);
  }

  protected void checkNullValue(ProductUIFormField aProductUIFormField) throws Exception {

    if (aProductUIFormField.getCode() == null) {
      throw new Exception("The ProductUIFormField.code value is not allow NULL");
    }

    if (aProductUIFormField.getTitle() == null) {
      throw new Exception("The ProductUIFormField.title value is not allow NULL");
    }

    if (aProductUIFormField.getTitleKey() == null) {
      throw new Exception("The ProductUIFormField.titleKey value is not allow NULL");
    }

    if (aProductUIFormField.getTipInfo() == null) {
      throw new Exception("The ProductUIFormField.tipInfo value is not allow NULL");
    }

    if (aProductUIFormField.getReadOnly() == null) {
      throw new Exception("The ProductUIFormField.readOnly value is not allow NULL");
    }

    if (aProductUIFormField.getAllowEmpty() == null) {
      throw new Exception("The ProductUIFormField.allowEmpty value is not allow NULL");
    }

    if (aProductUIFormField.getDataSource() == null) {
      throw new Exception("The ProductUIFormField.dataSource value is not allow NULL");
    }

    if (aProductUIFormField.getStyle() == null) {
      throw new Exception("The ProductUIFormField.style value is not allow NULL");
    }

    if (aProductUIFormField.getParameterValue() == null) {
      throw new Exception("The ProductUIFormField.parameterValue value is not allow NULL");
    }

    if (aProductUIFormField.getStatus() == null) {
      throw new Exception("The ProductUIFormField.status value is not allow NULL");
    }

    if (aProductUIFormField.getRemarks() == null) {
      throw new Exception("The ProductUIFormField.remarks value is not allow NULL");
    }

    if (aProductUIFormField.getTypeWidget() == null) {
      throw new Exception("The ProductUIFormField.typeWidget value is not allow NULL");
    }

    if (aProductUIFormField.getTypeName() == null) {
      throw new Exception("The ProductUIFormField.typeName value is not allow NULL");
    }

    if (aProductUIFormField.getAllowSubField() == null) {
      throw new Exception("The ProductUIFormField.allowSubField value is not allow NULL");
    }

    if (aProductUIFormField.getTypeIcon() == null) {
      throw new Exception("The ProductUIFormField.typeIcon value is not allow NULL");
    }
  }

  /**
   * Get Data By ID
   *
   * @param final long id * @return ${businessObject.name}
   * @throws Exception
   */
  public ProductUIFormField findById(final long id) throws Exception {

    ProductUIFormFieldDAOQueryBean queryBean = new ProductUIFormFieldDAOQueryBean();

    ProductUIFormFieldDAOQueryBean.Criteria criteria = queryBean.createCriteria();

    criteria.andIdEqualTo(id);

    // queryBean.setDistinct(true);

    ProductUIFormField[] ProductUIFormFields = query(queryBean);

    if (ProductUIFormFields.length == 0) {
      return null;
    } else {
      return ProductUIFormFields[0];
    }
  }

  /**
   * Query Data
   *
   * @param queryBean
   * @return ProductUIFormField[]
   * @throws Exception
   */
  public ProductUIFormField[] query(ProductUIFormFieldDAOQueryBean queryBean) throws Exception {

    List result = super.doQuery(queryBean, new JdbcProductUIFormFieldRowMapper());

    return (ProductUIFormField[]) result.toArray(new ProductUIFormField[result.size()]);
  }

  /**
   * Query ${businessObject.name} Selective
   *
   * @param queryBean
   * @return Map[]
   * @model
   * @throws Exception
   */
  public Map<String, Object>[] querySelective(ProductUIFormFieldDAOQueryBean queryBean)
      throws Exception {

    if (queryBean.getSelectProperties().isEmpty()) {
      queryBean.addAllSelectProperties();
    }

    return super.doQuerySelective(queryBean);
  }

  /**
   * Query Data Count
   *
   * @param queryBean
   * @return int
   * @throws Exception
   */
  public int queryCount(ProductUIFormFieldDAOQueryBean queryBean) throws Exception {

    return super.doQueryCount(queryBean);
  }

  public void synchronize(
      ProductUIFormField[] aProductUIFormFields, ProductUIFormFieldDAOQueryBean queryBean)
      throws Exception {
    daoHelper.synchronize(aProductUIFormFields, queryBean);
  }

  public void synchronize(
      ProductUIFormField[] aProductUIFormFields,
      ProductUIFormFieldDAOQueryBean queryBean,
      net.dao.IDataAccessInterface.ISynchronizeCallback<ProductUIFormField> callBack)
      throws Exception {
    daoHelper.synchronize(aProductUIFormFields, queryBean, callBack);
  }

  public static class JdbcProductUIFormFieldRowMapper implements RowMapper<ProductUIFormField> {

    public ProductUIFormField mapRow(ResultSet resultSet, int arg1) throws SQLException {
      ProductUIFormField object = new ProductUIFormField();

      int columnIndex = 1;

      int __dataType;

      try {

        Object id = null;

        id = resultSet.getObject(columnIndex++);

        object.setIdNull(resultSet.getBoolean(columnIndex++));

        if (id != null) {

          setBeanValue(object, _DataObjectProperty.Id, id);
        }

        Object parentId = null;

        parentId = resultSet.getObject(columnIndex++);

        object.setParentIdNull(resultSet.getBoolean(columnIndex++));

        if (parentId != null) {

          setBeanValue(object, _DataObjectProperty.ParentId, parentId);
        }

        Object formId = null;

        formId = resultSet.getObject(columnIndex++);

        object.setFormIdNull(resultSet.getBoolean(columnIndex++));

        if (formId != null) {

          setBeanValue(object, _DataObjectProperty.FormId, formId);
        }

        Object typeId = null;

        typeId = resultSet.getObject(columnIndex++);

        object.setTypeIdNull(resultSet.getBoolean(columnIndex++));

        if (typeId != null) {

          setBeanValue(object, _DataObjectProperty.TypeId, typeId);
        }

        Object code = null;

        code = resultSet.getObject(columnIndex++);

        object.setCodeNull(resultSet.getBoolean(columnIndex++));

        if (code != null) {

          setBeanValue(object, _DataObjectProperty.Code, code);
        }

        Object title = null;

        title = resultSet.getObject(columnIndex++);

        object.setTitleNull(resultSet.getBoolean(columnIndex++));

        if (title != null) {

          setBeanValue(object, _DataObjectProperty.Title, title);
        }

        Object titleKey = null;

        titleKey = resultSet.getObject(columnIndex++);

        object.setTitleKeyNull(resultSet.getBoolean(columnIndex++));

        if (titleKey != null) {

          setBeanValue(object, _DataObjectProperty.TitleKey, titleKey);
        }

        Object tipInfo = null;

        tipInfo = resultSet.getObject(columnIndex++);

        object.setTipInfoNull(resultSet.getBoolean(columnIndex++));

        if (tipInfo != null) {

          setBeanValue(object, _DataObjectProperty.TipInfo, tipInfo);
        }

        Object readOnly = null;

        readOnly = resultSet.getObject(columnIndex++);

        object.setReadOnlyNull(resultSet.getBoolean(columnIndex++));

        if (readOnly != null) {

          setBeanValue(object, _DataObjectProperty.ReadOnly, readOnly);
        }

        Object allowEmpty = null;

        allowEmpty = resultSet.getObject(columnIndex++);

        object.setAllowEmptyNull(resultSet.getBoolean(columnIndex++));

        if (allowEmpty != null) {

          setBeanValue(object, _DataObjectProperty.AllowEmpty, allowEmpty);
        }

        Object dataSource = null;

        dataSource = resultSet.getObject(columnIndex++);

        object.setDataSourceNull(resultSet.getBoolean(columnIndex++));

        if (dataSource != null) {

          setBeanValue(object, _DataObjectProperty.DataSource, dataSource);
        }

        Object style = null;

        style = resultSet.getObject(columnIndex++);

        object.setStyleNull(resultSet.getBoolean(columnIndex++));

        if (style != null) {

          setBeanValue(object, _DataObjectProperty.Style, style);
        }

        Object parameterValue = null;

        parameterValue = resultSet.getObject(columnIndex++);

        object.setParameterValueNull(resultSet.getBoolean(columnIndex++));

        if (parameterValue != null) {

          setBeanValue(object, _DataObjectProperty.ParameterValue, parameterValue);
        }

        Object sortIndex = null;

        sortIndex = resultSet.getObject(columnIndex++);

        object.setSortIndexNull(resultSet.getBoolean(columnIndex++));

        if (sortIndex != null) {

          setBeanValue(object, _DataObjectProperty.SortIndex, sortIndex);
        }

        Object status = null;

        status = resultSet.getObject(columnIndex++);

        object.setStatusNull(resultSet.getBoolean(columnIndex++));

        if (status != null) {

          setBeanValue(object, _DataObjectProperty.Status, status);
        }

        Object remarks = null;

        remarks = resultSet.getObject(columnIndex++);

        object.setRemarksNull(resultSet.getBoolean(columnIndex++));

        if (remarks != null) {

          setBeanValue(object, _DataObjectProperty.Remarks, remarks);
        }

        Object modelPropertyId = null;

        modelPropertyId = resultSet.getObject(columnIndex++);

        object.setModelPropertyIdNull(resultSet.getBoolean(columnIndex++));

        if (modelPropertyId != null) {

          setBeanValue(object, _DataObjectProperty.ModelPropertyId, modelPropertyId);
        }

        Object fieldSpan = null;

        fieldSpan = resultSet.getObject(columnIndex++);

        object.setFieldSpanNull(resultSet.getBoolean(columnIndex++));

        if (fieldSpan != null) {

          setBeanValue(object, _DataObjectProperty.FieldSpan, fieldSpan);
        }

        Object fieldColumn = null;

        fieldColumn = resultSet.getObject(columnIndex++);

        object.setFieldColumnNull(resultSet.getBoolean(columnIndex++));

        if (fieldColumn != null) {

          setBeanValue(object, _DataObjectProperty.FieldColumn, fieldColumn);
        }

        Object remenberValue = null;

        remenberValue = resultSet.getObject(columnIndex++);

        object.setRemenberValueNull(resultSet.getBoolean(columnIndex++));

        if (remenberValue != null) {

          setBeanValue(object, _DataObjectProperty.RemenberValue, remenberValue);
        }

        Object typeWidget = null;

        typeWidget = resultSet.getObject(columnIndex++);

        object.setTypeWidgetNull(resultSet.getBoolean(columnIndex++));

        if (typeWidget != null) {

          setBeanValue(object, _DataObjectProperty.TypeWidget, typeWidget);
        }

        Object typeName = null;

        typeName = resultSet.getObject(columnIndex++);

        object.setTypeNameNull(resultSet.getBoolean(columnIndex++));

        if (typeName != null) {

          setBeanValue(object, _DataObjectProperty.TypeName, typeName);
        }

        Object allowSubField = null;

        allowSubField = resultSet.getObject(columnIndex++);

        object.setAllowSubFieldNull(resultSet.getBoolean(columnIndex++));

        if (allowSubField != null) {

          setBeanValue(object, _DataObjectProperty.AllowSubField, allowSubField);
        }

        Object typeIcon = null;

        typeIcon = resultSet.getObject(columnIndex++);

        object.setTypeIconNull(resultSet.getBoolean(columnIndex++));

        if (typeIcon != null) {

          setBeanValue(object, _DataObjectProperty.TypeIcon, typeIcon);
        }

        Object dataSourceCode = null;

        dataSourceCode = resultSet.getObject(columnIndex++);

        object.setDataSourceCodeNull(resultSet.getBoolean(columnIndex++));

        if (dataSourceCode != null) {

          setBeanValue(object, _DataObjectProperty.DataSourceCode, dataSourceCode);
        }

        Object dataSourceName = null;

        dataSourceName = resultSet.getObject(columnIndex++);

        object.setDataSourceNameNull(resultSet.getBoolean(columnIndex++));

        if (dataSourceName != null) {

          setBeanValue(object, _DataObjectProperty.DataSourceName, dataSourceName);
        }

        return object;
      } catch (Exception e) {
        throw new SQLException(e.getMessage());
      }
    }
  }
}
